<?php

    require_once(dirname(__FILE__) . '/../includes/common.inc.php');

    define( "LOGIN_ERROR", "login_error" );
    define( 'HOME_PAGE_URL', Pages::GetPageUrl("AdminArticles"));

    $user = Core::App()->GetUser();

    $isLogOut = Request::GET( "logout" );
    if ( !empty( $isLogOut) )
    {
        $user->LogOut();

        Request::Redirect( $user->GetLoginUrl() );
    }

    if ( $user->IsAuthenticated() )
    {
        Request::Redirect( HOME_PAGE_URL );
    }

    $form = new AdminLoginForm( );

    $page = Pages::CreatePage( $pageInfo->id );
    $error = "";
    if ( Request::IsMethod( Request::POST ) )
    {
        $form->Bind();
        if ( !$form->IsValid() || $form->HasErrors() )
        {
            $error = LOGGING_ERROR;
        }
        else
        {
            $username = $form->GetValue( "username" );

            if ($user->Authenticate( $form->GetValue( "username" ), $form->GetValue( "password" ) ))
            {
                $user->Login( $username, Request::GetParam( "remember_me" ) == "on" );

                Request::Redirect( $user->GetReturnUrl(HOME_PAGE_URL) );
            }
            else
            {
                $error = LOGIN_ERROR;
            }
        }        
    }

    $page->SetVar( 'FORM', $form->Generate() );
    $page->SetVar( 'LOGIN_ERROR', $error );
    $page->SetTopMenuVar( 'HIDE_LOGOUT', true );

    $page->ResetTop();
    $page->Display();

?>